import request from '@/utils/request'

// 发送消息给AI
export function sendMessageToAI(data) {
  return request({
    url: '/api/video/chat',
    method: 'post',
    data
  })
}

// 获取AI对话历史
export function getAIChatHistory() {
  return request({
    url: '/api/video/chat/history',
    method: 'get'
  })
}

// 清空AI对话历史
export function clearAIChatHistory() {
  return request({
    url: '/api/video/chat/history',
    method: 'delete'
  })
}

// 生成视频
export const generateVideo = async (data) => {
  return request({
    url: '/api/media-resources/generate-video',
    method: 'post',
    data: {
      subject: data.subject,
      grade: data.grade,
      topic: data.topic,
      description: data.description,
      duration: data.duration
    }
  })
}

// 获取视频生成历史记录
export const fetchVideoHistory = async () => {
  try {
    // 暂时返回空数组，等待后端接口实现
    return [];
    // const response = await request.get('/api/video/history');
    // return response.data;
  } catch (error) {
    console.error('获取视频历史记录失败:', error);
    return [];
  }
};

// 下载视频
export function downloadVideo(videoId) {
  return request({
    url: `/api/video/download/${videoId}`,
    method: 'get',
    responseType: 'blob'
  })
}

// 预览视频
export function previewVideo(videoId) {
  return request({
    url: `/api/video/preview/${videoId}`,
    method: 'get'
  })
}

// 查询视频生成任务状态
export function getVideoTaskStatus(taskId) {
  return request({
    url: `/api/video/task/${taskId}`,
    method: 'get'
  })
}

// 直接调用阿里云通义万相API创建视频生成任务
export function createWanxVideoTask(data) {
  return request({
    url: '/api/video/wanx/create',
    method: 'post',
    data: {
      ...data,
      model: 'wanx2.1-t2v-turbo'
    }
  })
}

// 直接调用阿里云通义万相API查询视频生成任务状态
export function getWanxVideoTaskStatus(taskId) {
  return request({
    url: `/api/media-resources/task/${taskId}`,
    method: 'get'
  })
}